#include "gxplaylist.h"
#include <gntstyle.h>

static void
playlist_activate(GntWidget *wid, gpointer null)
{
	SongInfo *info;
	GxPlayList *pl = GX_PLAY_LIST(wid);
	int pos = GPOINTER_TO_INT(gnt_tree_get_selection_data(GNT_TREE(wid)));
	info = g_list_nth_data(pl->songs, pos - 1);
	if (info) { /* XXX: This should never fail */
		if (pl->current.info)
			gnt_tree_set_row_flags(GNT_TREE(wid), GINT_TO_POINTER(pl->current.pos + 1), GNT_TEXT_FLAG_NORMAL);
		gx_jump(pos - 1);
	}
}

static gboolean
gxp_play(GntBindable *bind, GList * null)
{
	gx_play();
	return TRUE;
}

static gboolean
gxp_pause(GntBindable *bind, GList * null)
{
	gx_pause();
	return TRUE;
}

static gboolean
gxp_stop(GntBindable *bind, GList * null)
{
	gx_stop();
	return TRUE;
}

static gboolean
gxp_next(GntBindable *bind, GList * null)
{
	gx_play_next();
	return TRUE;
}

static gboolean
gxp_prev(GntBindable *bind, GList * null)
{
	gx_play_prev();
	return TRUE;
}

GntWidget *gx_playlist_new(const char *name)
{
	GntWidget *wid = g_object_new(GX_TYPE_PLAYLIST, NULL);
	GxPlayList *pl = GX_PLAY_LIST(wid);
	pl->name = g_strdup(name);
	
	_gnt_tree_init_internals(GNT_TREE(wid), 2);
	gnt_tree_set_show_title(GNT_TREE(wid), TRUE);
	gnt_tree_set_column_titles(GNT_TREE(wid), "Pos", "Title", NULL);
	gnt_tree_set_col_width(GNT_TREE(wid), 0, P_WIDTH);
	gnt_tree_set_col_width(GNT_TREE(wid), 1, 40);
	GNT_WIDGET_SET_FLAGS(wid, GNT_WIDGET_NO_BORDER | GNT_WIDGET_CAN_TAKE_FOCUS);
	g_signal_connect(G_OBJECT(wid), "activate", G_CALLBACK(playlist_activate), NULL);

	return wid;
}

static void gx_playlist_class_init(GxPlayListClass *klass)
{
	GntBindableClass *bindable = GNT_BINDABLE_CLASS(klass);
	gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "play",  gxp_play, "x", NULL);
	gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "pause", gxp_pause, "c", NULL);
	gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "stop",  gxp_stop, "v", NULL);
	gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "prev",  gxp_prev, "z", NULL);
	gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "next",  gxp_next, "b", NULL);
	gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), bindable);
}

GType gx_get_gtype(void)
{
	static GType type = 0;

	if(type == 0) {
		static const GTypeInfo info = {
			sizeof(GxPlayListClass),
			NULL,					/* base_init		*/
			NULL,					/* base_finalize	*/
			(GClassInitFunc)gx_playlist_class_init,
			NULL,
			NULL,                   /* class_data		*/
			sizeof(GxPlayList),
			0,                      /* n_preallocs		*/
			NULL,	            /* instance_init	*/
			NULL
		};

		type = g_type_register_static(GNT_TYPE_TREE,
									  "GxPlayList",
									  &info, 0);
	}

	return type;
}

void gx_playlist_update(GxPlayList *gxlist)
{
	GList *list = gx_list(NULL), *iter;
	int counter = 1;
	gpointer sel;

	sel = gnt_tree_get_selection_data(GNT_TREE(gxlist));
	gnt_tree_remove_all(GNT_TREE(gxlist));

	for (iter = list; iter; iter = iter->next, counter++) {
		char pos[32];
		SongInfo *info = iter->data;
		/* XXX: for now, we don't allow the same song to be added more than once :( */
		snprintf(pos, sizeof(pos), "%*d", P_WIDTH, counter);
		gx_song_info_ref(info);
		gnt_tree_add_row_last(GNT_TREE(gxlist), GINT_TO_POINTER(counter),
				gnt_tree_create_row(GNT_TREE(gxlist), pos, info->title), NULL);
		g_hash_table_replace(songs, &info->mlibid, info);
	}

	gnt_tree_set_selected(GNT_TREE(gxlist), sel);
	g_list_foreach(gxlist->songs, (GFunc)gx_song_info_unref, NULL);
	g_list_free(gxlist->songs);
	gxlist->songs = list;
}

